//
//  CorrelationTableViewCell.m
//  Cooking
//
//  Created by ZhiPeng.Y on 15/9/2.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "CorrelationTableViewCell.h"
#import "AppMacro.h"
#import "CorreiationModel.h"
#import "UIImageView+WebCache.h"

@interface CorrelationTableViewCell ()

@property (retain, nonatomic) UIImageView *materialImage;
@property (retain, nonatomic) UIImageView *material;
@property (retain, nonatomic) UILabel     *nutritionLabel;
@property (retain, nonatomic) UILabel     *productionLabel;
@end

@implementation CorrelationTableViewCell

- (void)dealloc {
    [_materialImage release];
    [_material release];
    [_nutritionLabel release];
    [_productionLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    self.materialImage = [[UIImageView alloc] init];
//    _materialImage.backgroundColor = [UIColor grayColor];
    _materialImage.image = [self imageWithName:@"backGround" type:@"png"];
    [self.contentView addSubview:_materialImage];
    [_materialImage release];
    
    self.material = [[UIImageView alloc] init];
//    _material.backgroundColor = [UIColor purpleColor];
    [_materialImage addSubview:_material];
    [_material release];
    
    self.nutritionLabel = [[UILabel alloc] init];
//    _nutritionLabel.backgroundColor = [UIColor grayColor];
    _nutritionLabel.numberOfLines = 0;
    _nutritionLabel.font = [UIFont systemFontOfSize:14];
    _nutritionLabel.textColor = [UIColor brownColor];
    [self.contentView addSubview:_nutritionLabel];
    [_nutritionLabel release];
    
    self.productionLabel = [[UILabel alloc] init];
//    _productionLabel.backgroundColor = [UIColor yellowColor];
    _productionLabel.numberOfLines = 0;
    _productionLabel.font = [UIFont systemFontOfSize:14];
    _productionLabel.textColor = [UIColor brownColor];
    [self.contentView addSubview:_productionLabel];
    [_productionLabel release];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if ([UIScreen mainScreen].bounds.size.height == 667) {
        _materialImage.frame = CGRectMake(60, 10, WIDTH - 120, HEIGHT / 4);
        _material.frame = CGRectMake(10, 10, _materialImage.frame.size.width - 20, _materialImage.frame.size.height - 22.5);
        _nutritionLabel.frame = CGRectMake(10, _material.frame.origin.y + _material.frame.size.height + 5, WIDTH - 20, HEIGHT / 5);
        _productionLabel.frame = CGRectMake(10, _nutritionLabel.frame.origin.y + _nutritionLabel.frame.size.height + 2, WIDTH - 20, HEIGHT / 10);

    } else if ([UIScreen mainScreen].bounds.size.height == 568) {
        _materialImage.frame = CGRectMake(60, 10, WIDTH - 120, HEIGHT / 4);
        _material.frame = CGRectMake(10, 10, _materialImage.frame.size.width - 20, _materialImage.frame.size.height - 22.5);
        _nutritionLabel.frame = CGRectMake(10, _material.frame.origin.y + _material.frame.size.height + 5, WIDTH - 20, HEIGHT / 4);
        _productionLabel.frame = CGRectMake(10, _nutritionLabel.frame.origin.y + _nutritionLabel.frame.size.height + 2, WIDTH - 20, HEIGHT / 10);
        
    } else if ([UIScreen mainScreen].bounds.size.height == 736) {
        _materialImage.frame = CGRectMake(60, 10, WIDTH - 120, HEIGHT / 4);
        _material.frame = CGRectMake(10, 10, _materialImage.frame.size.width - 20, _materialImage.frame.size.height - 22.5);
        _nutritionLabel.frame = CGRectMake(10, _material.frame.origin.y + _material.frame.size.height + 5, WIDTH - 20, HEIGHT / 5);
        _productionLabel.frame = CGRectMake(10, _nutritionLabel.frame.origin.y + _nutritionLabel.frame.size.height + 2, WIDTH - 20, HEIGHT / 10);

    } else if ([UIScreen mainScreen].bounds.size.height == 480) {
        _materialImage.frame = CGRectMake(60, 10, WIDTH - 120, HEIGHT / 4);
        _material.frame = CGRectMake(10, 10, _materialImage.frame.size.width - 20, _materialImage.frame.size.height - 22.5);
        _nutritionLabel.frame = CGRectMake(10, _material.frame.origin.y + _material.frame.size.height + 5, WIDTH - 20, HEIGHT / 5);
        _productionLabel.frame = CGRectMake(10, _nutritionLabel.frame.origin.y + _nutritionLabel.frame.size.height + 2, WIDTH - 20, HEIGHT / 10);
        
    }


}
- (UIImage *)imageWithName:(NSString *)imageName type:(NSString *)imageType {
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:imageType];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}


- (void)sendValueWithModel:(CorreiationModel *)stepModel {
    [_material sd_setImageWithURL:[NSURL URLWithString:stepModel.imagePath] placeholderImage:[UIImage imageNamed:@"zhanwei_02.png"]];
    
    //实现首字放大
    NSMutableAttributedString *nutStr = [[NSMutableAttributedString alloc] initWithString:stepModel.nutritionAnalysis];
    [nutStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30.0f]} range:NSMakeRange(0, 1)];
    _nutritionLabel.attributedText = nutStr;
    
    NSMutableAttributedString *proStr = [[NSMutableAttributedString alloc] initWithString:stepModel.productionDirection];
    [proStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30.0f]} range:NSMakeRange(0, 1)];
    _productionLabel.attributedText = proStr;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
